package com.example.web_task.controller;


import com.example.web_task.constant.ExceptionConstants;
import com.example.web_task.entity.Result;
import com.example.web_task.entity.Teacher;
import com.example.web_task.exception.ParameterException;
import com.example.web_task.exception.TeacherNotExistException;
import com.example.web_task.service.ITeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学院老师 前端控制器
 * </p>
 *
 * @author suheng
 * @since 2024-02-23
 */
@RestController
@RequestMapping("/teacher")
@Api(tags = "教师相关接口")
@Slf4j
public class TeacherController {
    @Autowired
    private ITeacherService iTeacherService;
    /*
    新增一名教师
     */
    @PostMapping
    @ApiOperation("新增一名教师")
    public Result<String>insert(@RequestBody Teacher teacher){
        log.info("新增一名教师");
        if(teacher==null){
            throw new ParameterException(ExceptionConstants.PARAMMETER_ERROR);
        }
        boolean save = iTeacherService.save(teacher);
        log.info("save="+save);
        return Result.success();
    }
    /*
    根据id删除教师
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除一名教师")
    public Result<String>deleteById(@PathVariable Long id){
        log.info("删除一名教师");
        if(id==null){
            throw new ParameterException(ExceptionConstants.PARAMMETER_ERROR);
        }
        boolean isSuccess = iTeacherService.removeById(id);
        if(!isSuccess){
            throw new TeacherNotExistException(ExceptionConstants.TEACHER_NOT_EXIST);
        }
        return Result.success();
    }
    /*
    根据id查询教师信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询教师信息")
    public Result<Teacher>getById(@PathVariable Long id){
        log.info("查询教师信息 id="+id);
        if(id==null){
            throw new ParameterException(ExceptionConstants.PARAMMETER_ERROR);
        }
        Teacher teacher = iTeacherService.getById(id);
        if(teacher==null){
            throw new TeacherNotExistException(ExceptionConstants.TEACHER_NOT_EXIST);
        }
        return Result.success(teacher);
    }
    /*
    修改教师信息
     */
    @PutMapping
    @ApiOperation("修改教师信息")
    public Result<String>update(@RequestBody Teacher teacher){
        log.info("修改教师信息");
        if(teacher==null){
            throw new ParameterException(ExceptionConstants.PARAMMETER_ERROR);
        }
        boolean isSuccess = iTeacherService.updateById(teacher);
        if(!isSuccess){
            throw new TeacherNotExistException(ExceptionConstants.TEACHER_NOT_EXIST);
        }
        return Result.success();
    }
}
